package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_org;

import cn.com.api.service.ApiU_orgService;

import cn.com.util.*;
import cn.com.util.*;

public class U_orgBaseController {

	private static Logger logger = Logger.getLogger(U_orgBaseController.class);

	@Autowired
	private ApiU_orgService apiU_orgService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_org-list")
	public ModelAndView u_org_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_org_list.jsp");

		List<ApiU_org> u_orgs = apiU_orgService.findU_orgs();
		mv.addObject("u_orgs", u_orgs);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_org-add-pre")
	public ModelAndView u_org_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_org_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiU_org u_org = apiU_orgService.findU_orgById(Integer.valueOf(id));
			mv.addObject("u_org", u_org);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_org-add")
	public void u_org_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String id = request.getParameter("orgid");
			String corp_code = request.getParameter("corp_code");
			String cur_index = request.getParameter("cur_index");
			String corp_fname = request.getParameter("corp_fname");
			String corp_sname = request.getParameter("corp_sname");
			String belong = request.getParameter("belong");
			String address = request.getParameter("address");
			String corp_size = request.getParameter("corp_size");
			String url = request.getParameter("url");
			String logo = request.getParameter("logo");
			String description = request.getParameter("description");
			String back_img = request.getParameter("back_img");

			if (id == null || "".equals(id.trim())) {// add
				ApiU_org u_org = new ApiU_org();
				u_org.setCorp_code(corp_code);
				u_org.setCorp_fname(corp_fname);
				u_org.setCorp_sname(corp_sname);
				u_org.setBelong(belong);
				u_org.setAddress(address);
				u_org.setCorp_size(corp_size);
				u_org.setUrl(url);
				u_org.setLogo(logo);
				if (description != null && !"".equals(description)) {
					u_org.setDescription(description.trim());
				}
				u_org.setBack_img(back_img);
				apiU_orgService.insertU_org(u_org);
			} else {// edit
				ApiU_org u_org = apiU_orgService.findU_orgById(Integer.valueOf(id));
				u_org.setCorp_code(corp_code);
				u_org.setCorp_fname(corp_fname);
				u_org.setCorp_sname(corp_sname);
				u_org.setBelong(belong);
				u_org.setAddress(address);
				u_org.setCorp_size(corp_size);
				u_org.setUrl(url);
				u_org.setLogo(logo);
				if (description != null && !"".equals(description)) {
					u_org.setDescription(description.trim());
				}
				u_org.setBack_img(back_img);
				apiU_orgService.updateU_org(u_org);
			}
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "添加成功！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_org-delete")
	public void u_org_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String id = request.getParameter("id");
			int ret_i = apiU_orgService.deleteU_org(Integer.valueOf(id));
			if (ret_i > 0) {
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "删除成功！");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
				map.put(Utils.MESSAGE, "删除失败！");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
